package com.yunhe.policyfulfillment.policyful.controller;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.authority.domain.PolicyUser;
import com.yunhe.policyfulfillment.constant.PolicyFulfillmentConstant;
import com.yunhe.policyfulfillment.policyful.domain.PolicyInfoMatter;
import com.yunhe.policyfulfillment.policyful.domain.PolicyInfoMatter;
import com.yunhe.policyfulfillment.policyful.model.PolicyInfoMatterQueryCondition;
import com.yunhe.policyfulfillment.policyful.model.PolicyInfoMatterVo;
import com.yunhe.policyfulfillment.policyful.model.PublishStatus;
import com.yunhe.policyfulfillment.policyful.service.IPolicyInfoMatterService;
import com.yunhe.policyfulfillment.util.MyHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName PolicyInfoMatterController
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/26 14:23
 * @Version 1.0
 **/
@Api(value = "PolicyInfoMatter", description = "事项维护", tags = {"Model_PolicyInfoMatter"})
@RestController
@RequestMapping(value = "/policyInfoMatter")
public class PolicyInfoMatterController {

    @Autowired
    private IPolicyInfoMatterService policyInfoMatterService;

    @Autowired
    private MyHeaderUtil myHeaderUtil;
    /**
     * 刪除
     * @author hetao
     * @
     * date 2021/07/26
     **/
    @ApiOperation("根据id删除事项")
    @DeleteMapping(value = "/deletePolicyInfoMatter")
    public ResponseEntity<ResultObject<Integer>> delete(@RequestParam(value = "id",required = true) long id) throws ArgumentErrorException {
        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        if(!PolicyFulfillmentConstant.Role_Name.SupervisorOrg.value().equals(headerUser.getRoleName())){
            throw new ArgumentErrorException("当前登录用户不是主管部门用户不能进行删除事项！");
        }
        policyInfoMatterService.deletePolicyInfoMatter(id);
        return new ResponseEntity<>(new ResultObject(), HttpStatus.OK);
    }

    @ApiOperation("新增事项")
    @PostMapping("/createPolicyInfoMatter")
    public ResponseEntity<ResultObject<Integer>> createPolicyInfoMatter(@RequestBody PolicyInfoMatter policyInfoMatter) throws ArgumentErrorException, PinyinException {
        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        if(!PolicyFulfillmentConstant.Role_Name.SupervisorOrg.value().equals(headerUser.getRoleName())){
            throw new ArgumentErrorException("当前登录用户不是主管部门用户不能进行新增事项！");
        }
        return new ResponseEntity<>(new ResultObject(policyInfoMatterService.createPolicyInfoMatter(policyInfoMatter)), HttpStatus.OK);
    }

    /**
     * 更新
     * @date 2021/07/23
     **/
    @ApiOperation("更新事项")
    @PostMapping("/updatePolicyInfoMatter")
    public ResponseEntity<ResultObject<Integer>> updatePolicyInfoMatter(@RequestBody PolicyInfoMatter policyInfoMatter) throws ArgumentErrorException, PinyinException {
        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        if(!PolicyFulfillmentConstant.Role_Name.SupervisorOrg.value().equals(headerUser.getRoleName())){
            throw new ArgumentErrorException("当前登录用户不是主管部门用户不能进行更新事项！");
        }
        policyInfoMatter.setAccessCount(null);
        policyInfoMatter.setDeclaredQuantity(null);
        return new ResponseEntity<>(new ResultObject(policyInfoMatterService.updatePolicyInfoMatter(policyInfoMatter)), HttpStatus.OK);
    }

    /**
     * 查询 根据主键 id 查询
     * @date 2021/07/23
     **/
    @ApiOperation("查询 事项")
    @GetMapping("/findPolicyInfoMatterById")
    public ResponseEntity<ResultObject<PolicyInfoMatter>> findPolicyInfoMatterById(@RequestParam(value = "id",required = true) long id){
        return new ResponseEntity<>(new ResultObject(policyInfoMatterService.findPolicyInfoMatterById(id)), HttpStatus.OK);
    }

    /**
     * 查询 分页查询
     * @date 2021/07/23
     **/
    @ApiOperation("分页查询")
    @PostMapping("/findPolicyInfoMatterByQueryCondition")
    public ResponseEntity<ResultObject<List<PolicyInfoMatterVo>>> findPolicyInfoMatterByQueryCondition(@RequestBody PolicyInfoMatterQueryCondition PolicyInfoMatterQueryCondition) throws ArgumentErrorException {
        PageResult pageResult = policyInfoMatterService.findPolicyInfoMatterByQueryCondition(PolicyInfoMatterQueryCondition);
        return new ResponseEntity<>(new ResultObject(pageResult), HttpStatus.OK);
    }

    /**
     * 设置发布状态
     * @date 2021/07/23
     **/
    @ApiOperation("设置发布状态")
    @PostMapping("/setPublishStatus")
    public ResponseEntity<ResultObject> setPublishStatus(@RequestBody PublishStatus publishStatus) throws ArgumentErrorException {
        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        if(!PolicyFulfillmentConstant.Role_Name.SupervisorOrg.value().equals(headerUser.getRoleName())){
            throw new ArgumentErrorException("当前登录用户不是主管部门用户不能设置状态！");
        }
        policyInfoMatterService.setPublishStatus(publishStatus);
        return new ResponseEntity<>(new ResultObject(), HttpStatus.OK);
    }
}
